<template>
  <div class="login-page">
    <van-nav-bar
    title="登录"
    />
    <h1 class="title">优点达资讯</h1>

    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        label="用户名"
        placeholder="请输入手机号"
        :rules="[{ required: true, pattern, message: '请输入正确手机号' }]"
        autocomplete="off"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, validator, message: '请填写正确密码,最少6个字符' }]"
        autocomplete="off"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
      <router-link to="/register">没有账号，立即注册</router-link>
    </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { loginAPI } from '@/api/user'
export default {
  name: 'LoginPage',
  data () {
    return {
      mobile: '',
      password: '',
      pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    }
  },
  methods: {
    validator (val) {
      return /^\S{6,}$/.test(val)
    },
    async onSubmit (values) {
      const { data } = await loginAPI(values)
      if (data.erron !== 0) {
        this.mobile = this.password = ''
        localStorage.setItem('mobile-token-ydd', data.data.token)
        Toast('登录成功')
        this.$router.push('/user')
      } else {
        Toast(data.errmsg)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #1989fa;
  // 导航文字
  /deep/ .van-nav-bar__title {
  color: white;
  font-weight: 500;
  font-size: 0.42667rem;
 }
  //修改箭头颜色
  /deep/ .van-icon-arrow-left {
  color: var(--nav-bar-icon-color, #fff) !important;
 }
}
.title {
  font-size: .8rem;
  color: #1989fa;
  font-weight: 400;
  text-align: center;
}
.login-page a {
  float: right;
  margin-right: 20px;
}
</style>
